#------------------------------------------------------------------------------
#   File:       lib_tex2text.py
#   Purpose:    
#   Author:     James Mynderse
#   Revised:
#   License:    GPLv3 see LICENSE.TXT    
#------------------------------------------------------------------------------

import re

#--[ Begin LaTeX Commands ]----------------------------------------------------
command_str = r'\\(?P<command>.[^{])\[(?P<parameter>.\]\{(?P<content>.[^}])\}'
command_obj = re.compile(command_str)
lbrace_str = r'\\\{'
lbrace_obj = re.compile(lbrace_str)
rbrace_str = r'\\\}'
rbrace_obj = re.compile(rbrace_str)
#include_str = r'\\include\{(?P<texfile>.+)\}'
#include_obj = re.compile(include_str)
#input_str = r'\\input\{(?P<texfile>.+)\}'
#input_obj = re.compile(input_str)
#emph_str = r'\\emph\{(?P<content>.[^}]*)\}'
#emph_obj = re.compile(emph_str)
#--[ End LaTeX Commands ]------------------------------------------------------

#--[ Begin Tex Class ]---------------------------------------------------------
class Tex(object):

    def __init__(self,texfile,newfile):
        # Tex File
        self.TexFile = texfile
        # New File        
        self.NewFile = newfile
        # Current line
        self.CurLine = ''
        return
    
    def ParseFile(self):
        # Scan each line
        for line_numer, line in enumerate(self.TexFile):
            print ('%s') % (line)
            
            # Replace left and right braces in text with parenthesis
            line = lbrace_obj.sub('(',line)
            line = rbrace_obj.sub(')',line)
            # Check for presence of a command
            match_obj = command_obj.search(line)
            # No command, move along
            if (match_obj is None):
                return
            
            print ('%s %s') % (match_obj.group('command'),match_obj.group('content')) 
            
            # Command here, deal with it
            TexCommands = {
                'documentclass':    line = self.TexIgnoreCommand(line),
                'emph':             line = self.TexIgnoreCommand(line),
                'include':          self.TexInclude(match_obj.group('content')),
                'input':            self.TexInput(match_obj.group('content')),
                'usepackage':       line = self.TexIgnore(line)
                'begin':            line = self.TexBeginSomething(match_obj.group('content')),
            }
            TexCommands.get(match_obj.group('command'))
            # Write the line to file
            self.WriteLine(line)
        return

    def TexIgnore(self,line):
        line = command_obj.sub('',line)
        return line

    def TexIgnoreCommand(self,line):
        match_obj = command_obj.search(line)
        line = command_obj.sub('',match_obj.group('content'))
        return line
        
    def TexInclude(self,texfile):
        self.WriteLine()
        temp = Tex(self.TexFile,self.NewFile)
        return
        
    def TexInput(self,texfile):
        temp = Tex(self.TexFile,self.NewFile)
        return
    
    def WriteLine(self,line):
        self.NewFile.write(line)
        self.CurLine = ''
        return
        
    def CloseFiles(self):
        self.TexFile.close()
        self.NewFile.close()
        return
    
#--[ End Tex Class ]-----------------------------------------------------------        


#--[ Begin Token Classes ]-----------------------------------------------------
#--[ End Token Classes ]-------------------------------------------------------

        
